package com.lib76.buildCode.service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.lib76.DBInfo.service.DatabaseInfoDaoFactory;
import com.lib76.DBInfo.service.DatabaseTypeEnum;
import com.lib76.DBInfo.service.IDatabaseInfoDao;
import com.lib76.buildCode.dto.DbLinkInfo;

public class DbInfoDaoManager {

	private final static ConcurrentMap<String,IDatabaseInfoDao> STORE = new ConcurrentHashMap<String,IDatabaseInfoDao>();
	
	public static String buildKey(DbLinkInfo linkInfo){
		return buildKey(linkInfo.getIp(),linkInfo.getPort(),linkInfo.getUserName());
	}
	
	public static String buildKey(String ip,Integer port,String userName){
		return ip.replace(".", "-")+"-"+port+"-"+userName;
	}
	
	public static IDatabaseInfoDao createDbInfoDao(DbLinkInfo linkInfo){
		String key = buildKey(linkInfo);
		IDatabaseInfoDao dao = getDbInfoDao(key);
		if(dao == null){
			dao = DatabaseInfoDaoFactory.createDatabaseInfoDao(DatabaseTypeEnum.MYSQL,linkInfo.getIp(),
					linkInfo.getPort(), linkInfo.getUserName(),linkInfo.getUserPwd());
			addDbInfoDao(key,dao);
		}
		return dao;
	}
	
	public static void addDbInfoDao(String key,IDatabaseInfoDao dao){
		STORE.put(key, dao);
	}
	
	public static IDatabaseInfoDao getDbInfoDao(String key){
		return STORE.get(key);
	}
}
